<?php
if ( count( get_included_files() ) == 1 ) die( '---' );

class Modele
{
	var $modele_id;
	
	var $createur;
	
	var $date_creation;
	
	var $course_code;
	
	var $nom;
	
	var $table;
	
	var $table_modele_user;
	
	var $table_modele_class;
	
	// Constructeur
	function Modele() 
    {
    	$this->course_code = $course_code;
    	$this->createur = $createur;
    	$this->nom = $nom;
    	$this->modele_id = -1;
    	
		global $tables;    	
		$this->table = $tables['PFLfiche'];
		$this->table_modele_user = $tables['PFLfiche_user'];
		$this->table_modele_class = $tables['PFLfiche_class'];
    }
    
    // Load 
    function load($modele_id)
    {
 
    	$sql		   = "SELECT fiche_id, createur, date_creation, course_code, nom
							FROM `".$this->table."`
							WHERE fiche_id = '".$modele_id."'";
		$data = claro_sql_query_get_single_row($sql);

	   if( !empty($data) )
	   {
	   	$this->modele_id     = $data['fiche_id'];
	   	$this->createur      = $data['createur'];
	   	$this->date_creation = $data['date_creation'];
	   	$this->course_code   = $data['course_code'];
	   	$this->nom				= $data['nom'];
	   	
	   	return true;
	   }
	   else
	   return false;
    }
    
    
    // Save 
    function save()  
    {
    	if( $this->modele_id == -1 )
    	{
    		$sql = "INSERT INTO `".$this->table."` 
					  SET createur = ".$this->createur.",
							date_creation = NOW(),
							course_code = '".$this->course_code."',
							nom = '".addslashes($this->nom)."'";
			// execute the creation query and get id of inserted assignment
		    $insertedId = claro_sql_query_insert_id($sql);

		    if( $insertedId )
		    {
		    	$this->modele_id = (int) $insertedId;

		      return $this->modele_id;
		    }
		    else
		    {
		        return false;
		    }
    	}
    	else
    	{
    		$sql = "UPDATE `".$this->table."` 
					  SET createur = ".$this->createur.",
							date_creation = NOW(),
							course_code = '".$this->course_code."',
							nom = '".addslashes($this->nom)."'
					  WHERE fiche_id = ".$this->modele_id;

			// execute and return main query
		    if( claro_sql_query($sql) )
		    {
		    	return $this->modele_id;
		    }
		    else
		    {
		    	return false;
		    }
    	}

    }
    
    function delete()
	 {
	 	$sql = "DELETE FROM `".$this->table."` WHERE fiche_id = ".$this->modele_id;
	 	if( claro_sql_query($sql) == false ) return false;
		
		$sql = "DELETE FROM `".$this->table_modele_user."` WHERE fiche_id = ".$this->modele_id;
	 	if( claro_sql_query($sql) == false ) return false;	
			
		$this->modele_id = -1;
		return true;
	 }
	 
	function chercherListeModele ()
	{
		$sql_search = "SELECT fiche_id
							FROM `".$this->table."`
							WHERE course_code = '".$this->course_code."'";

		return claro_sql_query_fetch_all_rows($sql_search);
	}
	
	
	function chercherListeInscrits()
	{
		$sql_search = "SELECT user_id
							FROM `".$this->table_modele_user."`
							WHERE fiche_id = ".$this->modele_id;
		return claro_sql_query_fetch_all_rows($sql_search);
	}	
	
	function chercherClassesInscrites()
	{
		$sql_search = "SELECT class_id
				 FROM `".$this->table_modele_class."`
				 WHERE fiche_id = '".$this->modele_id."'";
		return claro_sql_query_fetch_all_rows($sql_search);
	}	
	
	
	function supprimerUsers()
	{
		$sql = "DELETE FROM  `".$this->table_modele_user."`
					WHERE fiche_id = ".$this->modele_id;
		if( claro_sql_query($sql) == false ) return false;
	}
	
	function ajouterUser($userId)
	{
		$sql = "INSERT INTO `".$this->table_modele_user."`
					SET fiche_id = '".$this->modele_id."',
						   user_id = ".$userId;
		if( claro_sql_query($sql) == false ) return false;
	}
	
	function supprimerClasses()
	{
		$sql = "DELETE FROM  `".$this->table_modele_class."`
					WHERE fiche_id = ".$this->modele_id;
		if( claro_sql_query($sql) == false ) return false;
	}
	
	function ajouterClasse($classId)
	{
		$sql = "INSERT INTO `".$this->table_modele_class."`
					SET fiche_id = '".$this->modele_id."',
						   class_id = ".$classId;
		return claro_sql_query($sql);
	}

	
	/**
     * get id
     *
     * @author Pierre Raynaud
     * @return int
     */
	function getModeleId()
	{
		return (int) $this->modele_id;
	}
	
	/**
     * get createur
     *
     * @author Pierre Raynaud
     * @return int
     */
	function getCreateur()
	{
		return (int) $this->createur;
	}
	
	/**
     * set createur
     *
     * @author Pierre Raynaud
     * @param int $value
     */
	function setCreateur($value)
	{
		$this->createur = $value;
	}
	
	/**
     * get date_creation
     *
     * @author Pierre Raynaud
     * @return datetime
     */
	function getDateCreation()
	{
		return $this->date_creation;
	}
	
	/**
     * set date_creation
     *
     * @author Pierre Raynaud
     * @param $value
     */
	function setDateCreation($value)
	{
		$this->date_creation = $value;
	}
	
	/**
     * get nom
     *
     * @author Pierre Raynaud
     * @return string
     */
	function getNom()
	{
		return $this->nom;
	}
	
	/**
     * set nom
     *
     * @author Pierre Raynaud
     * @param string $value
     */
	function setNom($value)
	{
		$this->nom = trim($value);
		return $this->save();
	}
	
	/**
     * get course_code
     *
     * @author Pierre Raynaud
     * @return string
     */
	function getCourseCode()
	{
		return $this->course_code;
	}
	
	/**
     * set course_code
     *
     * @author Pierre Raynaud
     * @param string $value
     */
	function setCourseCode($value)
	{
		$this->course_code = trim($value);
	}

	/**
     * get heritage
     *
     * @author Pierre Raynaud
     * @return int
     */
	function getHeritage()
	{
		return $this->heritage;
	}
	
	/**
     * set heritage
     *
     * @author Pierre Raynaud
     * @param int $value
     */
	function setHeritage($value)
	{
		$this->heritage = (int) $value;
	}
}
?>
